package com.efast.dmom.mgmt.controller.les.pda;

import com.efast.cafe.framework.bean.ResultBean;
import com.efast.dmom.mgmt.bean.les.TwInvSkuLoctlBean;
import com.efast.dmom.mgmt.entity.les.TwInvSkuLoctl;
import com.efast.dmom.mgmt.service.les.TwInvSkuLoctlService;
import com.efast.dmom.mgmt.utils.Assist;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import java.util.List;

/**
 * @author ChenLiPeng
 * @date 2018年3月28日
 */
@Controller
@RequestMapping(value = "/pdaInventoryMovement")
@Api(description = "PDA->库内->库存移动执行-PDA")
public class PdaInventoryMovementController {

	private final TwInvSkuLoctlService twInvSkuLoctlService;

	@Autowired
	public PdaInventoryMovementController(TwInvSkuLoctlService twInvSkuLoctlService) {
		this.twInvSkuLoctlService = twInvSkuLoctlService;
	}

	@ApiOperation(value = "库存移动", notes = "请求参数：")
	@RequestMapping(value = "/move", method = RequestMethod.POST)
	public @ResponseBody
	ResultBean line(@RequestBody TwInvSkuLoctlBean[] twInvSkuLoctlBeans) {
		ResultBean result = new ResultBean(true, "'");
		for (TwInvSkuLoctlBean twInvSkuLoctlBean : twInvSkuLoctlBeans) {
			TwInvSkuLoctl dbTwInvSkuLoctl1 = twInvSkuLoctlService.selectTwInvSkuLoctlById(
					twInvSkuLoctlBean.getTwInvSkuLoctl().getTwInvSkuLoctlId());
			dbTwInvSkuLoctl1.setAvailableQty(twInvSkuLoctlBean.getTwInvSkuLoctl().getAvailableQty());
			dbTwInvSkuLoctl1.setLocNo(twInvSkuLoctlBean.getTwInvSkuLoctl().getLocNo());
			twInvSkuLoctlBean.setTwInvSkuLoctl(dbTwInvSkuLoctl1);
			twInvSkuLoctlService.inventoryMovement(twInvSkuLoctlBean);
		}
		result.setMessage("库存移动成功");
		return result;
	}

	@ApiOperation(value = "查询", notes = "请求参数：")
	@RequestMapping(value = "/query", method = RequestMethod.POST)
	public @ResponseBody ResultBean query(@RequestBody TwInvSkuLoctl twInvSkuLoctl) {
		ResultBean result = new ResultBean(true, "'");
		Assist assist = new Assist();
		if (twInvSkuLoctl.getPartNoVersion() != null) {
			assist.setRequires(Assist.andEq("part_no_version", twInvSkuLoctl.getPartNoVersion()));
		}
		if (twInvSkuLoctl.getLocNo() != null) {
			assist.setRequires(Assist.andEq("loc_no", twInvSkuLoctl.getLocNo()));
		}
		if (twInvSkuLoctl.getPartPackSerial() != null) {
			assist.setRequires(Assist.andEq("part_pack_serial", twInvSkuLoctl.getPartPackSerial()));
		}
		assist.setRequires(Assist.andNeq("loc_no", "stage"));
		List<TwInvSkuLoctl> list = twInvSkuLoctlService.selectTwInvSkuLoctl(assist);
		result.setObject(list);
		return result;
	}

}
